package com.todod.base.module.base.controller.admin.standards;

import com.todod.base.module.base.controller.admin.dto.ReportDataReq;
import com.todod.base.module.base.controller.admin.dto.ResultData;
import com.todod.base.module.base.util.FileZipUtils;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.CommonResult;
import com.todod.base.framework.common.util.object.BeanUtils;
import static com.todod.base.framework.common.pojo.CommonResult.success;

import com.todod.base.framework.excel.core.util.ExcelUtils;

import com.todod.base.framework.apilog.core.annotation.ApiAccessLog;
import static com.todod.base.framework.apilog.core.enums.OperateTypeEnum.*;

import com.todod.base.module.base.controller.admin.standards.vo.*;
import com.todod.base.module.base.dal.dataobject.standards.StandardsDO;
import com.todod.base.module.base.service.standards.StandardsService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 协议库")
@RestController
@RequestMapping("/base/standards")
@Validated
public class StandardsController {

    @Resource
    private StandardsService standardsService;

    @PostMapping("/create")
    @Operation(summary = "创建协议库")
    public CommonResult<Long> createStandards(@Valid @RequestBody StandardsSaveReqVO createReqVO) {
        return success(standardsService.createStandards(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新协议库")
    public CommonResult<Boolean> updateStandards(@Valid @RequestBody StandardsSaveReqVO updateReqVO) {
        standardsService.updateStandards(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除协议库")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteStandards(@RequestParam("id") Long id) {
        standardsService.deleteStandards(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得协议库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<StandardsRespVO> getStandards(@RequestParam("id") Long id) {
        StandardsDO standards = standardsService.getStandards(id);
        return success(BeanUtils.toBean(standards, StandardsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得协议库分页")
    public CommonResult<PageResult<StandardsRespVO>> getStandardsPage(@Valid StandardsPageReqVO pageReqVO) {
        PageResult<StandardsDO> pageResult = standardsService.getStandardsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StandardsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出协议库 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStandardsExcel(@Valid StandardsPageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StandardsDO> list = standardsService.getStandardsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "standards.xlsx", "数据", StandardsRespVO.class,
                BeanUtils.toBean(list, StandardsRespVO.class));
    }


    @GetMapping(value = "/downTemplate")
    @Operation(summary = "下载导入模版")
    public void problemTemplate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        standardsService.problemTemplate(request, response);
    }
    @PostMapping(value = "/uploadFile")
    @Operation(summary = "上传导入文件")
    public ResultData uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        return standardsService.uploadFile(file);
    }
    @PostMapping(value = "/reportDepartment")
    @Operation(summary = "执行导入文件")
    public ResultData reportDepartment(@RequestBody @Validated ReportDataReq reportDataReq) throws IOException {
        return standardsService.fileUnZip(reportDataReq);
    }
    @PostMapping(value = "/downloadWrongReport")
    @Operation(summary = "下载导入失败错误报告")
    public void downloadWrongReport(HttpServletResponse response, @RequestBody @Validated ReportDataReq reportDataReq) throws IOException {
        standardsService.downloadWrongReport(response, reportDataReq.getFileUrl());
    }




}